package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取课程安排信息
func GetWeekCourseInfo(p map[string]interface{}) *models.WeekCourse {
	o := orm.NewOrm()
	weekCourse := &models.WeekCourse{}
	qs := o.QueryTable("WeekCourse")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(weekCourse)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if weekCourse.Id > 0 {
			weekCourse.CreatedShow = weekCourse.Created.Format("2006-01-02 15:04:05")
		}
	}
	return weekCourse
}

// 添加课程安排信息
func AddWeekCourse(input url.Values) (int64, error) {
	o := orm.NewOrm()
	weekCourse := &models.WeekCourse{}

	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	weekCourse.User = user

	class := &models.Class{}
	if _, ok := input["class_id"]; ok {
		class.Id = utils.Atoi64(input["class_id"][0])
		o.Read(class)
	}
	weekCourse.Class = class

	if _, ok := input["monday_morning"]; ok {
		weekCourse.MondayMorning = input["monday_morning"][0]
	}
	if _, ok := input["monday_afternoon"]; ok {
		weekCourse.MondayAfternoon = input["monday_afternoon"][0]
	}

	if _, ok := input["tuesday_morning"]; ok {
		weekCourse.TuesdayMorning = input["tuesday_morning"][0]
	}
	if _, ok := input["tuesday_afternoon"]; ok {
		weekCourse.TuesdayAfternoon = input["tuesday_afternoon"][0]
	}

	if _, ok := input["wednesday_morning"]; ok {
		weekCourse.WednesdayMorning = input["wednesday_morning"][0]
	}
	if _, ok := input["wednesday_afternoon"]; ok {
		weekCourse.WednesdayAfternoon = input["wednesday_afternoon"][0]
	}

	if _, ok := input["thursday_morning"]; ok {
		weekCourse.ThursdayMorning = input["thursday_morning"][0]
	}
	if _, ok := input["thursday_afternoon"]; ok {
		weekCourse.ThursdayAfternoon = input["thursday_afternoon"][0]
	}

	if _, ok := input["thursday_morning"]; ok {
		weekCourse.ThursdayMorning = input["thursday_morning"][0]
	}
	if _, ok := input["thursday_afternoon"]; ok {
		weekCourse.ThursdayAfternoon = input["thursday_afternoon"][0]
	}

	if _, ok := input["friday_morning"]; ok {
		weekCourse.FridayMorning = input["friday_morning"][0]
	}
	if _, ok := input["friday_afternoon"]; ok {
		weekCourse.FridayAfternoon = input["friday_afternoon"][0]
	}

	if _, ok := input["saturday_morning"]; ok {
		weekCourse.SaturdayMorning = input["saturday_morning"][0]
	}
	if _, ok := input["saturday_afternoon"]; ok {
		weekCourse.SaturdayAfternoon = input["saturday_afternoon"][0]
	}

	if _, ok := input["sunday_morning"]; ok {
		weekCourse.SundayMorning = input["sunday_morning"][0]
	}
	if _, ok := input["sunday_afternoon"]; ok {
		weekCourse.SundayAfternoon = input["sunday_afternoon"][0]
	}

	return o.Insert(weekCourse)
}

/*// 从上传的excel添加课程
func AddWeekCourseFromExcel(input url.Values) (int64, error) {
	f, err := excelize.OpenFile("." + input["excel_file_path"][0])
	if err != nil {
		return -1, utils.NewError(0, "上传的excel文件无法打开，请重新上传")
	}
	o := orm.NewOrm()
	class := &models.Class{}
	if _, ok := input["class_id"]; ok {
		class.Id = utils.Atoi64(input["class_id"][0])
		o.Read(class)
	}
	var weekCourses []models.WeekCourse
	rows := f.GetRows("Sheet1")
	for k, row := range rows {
		if k == 0 {
			continue
		}
		weekCourse := models.WeekCourse{}
		weekCourse.Weekday = row[0]
		weekCourse.Morning = row[1]
		weekCourse.Afternoon = row[2]
		weekCourse.Class = class
		weekCourses = append(weekCourses, weekCourse)
	}
	return o.InsertMulti(1, weekCourses)
}*/

// 修改课程安排信息
func EditWeekCourse(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("WeekCourse").Filter("id", input["id"][0]).Update(p)
}

// 获取课程安排列表
func GetWeekCourseList(p map[string]interface{}) (int64, []*models.WeekCourse) {
	var weekCourseList []*models.WeekCourse
	o := orm.NewOrm()
	qs := o.QueryTable("WeekCourse")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	if _, ok := p["schoolId"]; ok {
		qs = qs.Filter("Class__School__Id", p["schoolId"])
	}
	if _, ok := p["classId"]; ok {
		qs = qs.Filter("class_id", p["classId"])
	}
	if _, ok := p["classIds"]; ok && len(p["classIds"].([]int64)) > 0 {
		qs = qs.Filter("class_id__in", p["classIds"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&weekCourseList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, weekCourse := range weekCourseList {
			weekCourse.CreatedDateShow = weekCourse.Created.Format("2006-1-2")
			weekCourse.CreatedShow = weekCourse.Created.Format("2006-01-02 15:04:05")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, weekCourse := range weekCourseList {
			weekCourse.ClassName = weekCourse.Class.Name
			weekCourse.CreatedDateShow = weekCourse.Created.Format("2006-1-2")
		}
	}
	return totalCount, weekCourseList
}